/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.edit.userdata;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.client.applicationframe.events.in.AddApplicationViewEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.edit.userdata.events.in.ShowEditUserDataViewEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.historytoken.client.HistoryToken;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.i18n.client.I18N;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.UserEditDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.edit.in.EditUserEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.edit.in.UpdateUserEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.edit.out.EMailInvalidEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.edit.out.PasswordEmptyEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.edit.out.ReceiveUserEditDTOEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.edit.out.UserUpdateSucessfullEvent;
import com.googlecode.hmvc4java.event.IEvent;
import com.googlecode.hmvc4java.registry.controller.AControllerModelViewRegistry;

/**
 * 
 * @author jens.meiss
 */
public class EditUserDataController extends AControllerModelViewRegistry<EditUserDataModel, EditUserDataView> {

	public void init() {
		
		register(ReceiveUserEditDTOEvent.class);
		register(PasswordEmptyEvent.class);
		register(EMailInvalidEvent.class);
		register(UserUpdateSucessfullEvent.class);
		register(ShowEditUserDataViewEvent.class);
		
	}

	public void terminate() {
	}

	public void handleEvent(IEvent event) {
		
		if (event.getClass() == ReceiveUserEditDTOEvent.class) {
			
			refreshView(((ReceiveUserEditDTOEvent) event).getEditDTO());
			
		} else if (event.getClass() == PasswordEmptyEvent.class) {
			
			passwordEmpty();
			
		} else if (event.getClass() == EMailInvalidEvent.class) {
			
			emailInvalid();
			
		} else if (event.getClass() == UserUpdateSucessfullEvent.class) {
			
			History.newItem(HistoryToken.dashboard);
			getComponent().terminateComponent();
			
		} else if (event.getClass() == ShowEditUserDataViewEvent.class) {
			
			fireEventGlobal(new EditUserEvent(this));
			fireEventGlobal(new AddApplicationViewEvent(null, getView().getWidget()));
			
			
		}
 		
	}

	/**
	 * 
	 */
	private void emailInvalid() {
		
		Label labelEMailInvalid = new Label(I18N.constants.labelEmailInvalid());
		getView().setLabelEMailInvalid(labelEMailInvalid);
		
		getView().getGridUserData().setWidget(1, 2, labelEMailInvalid);
		
	}

	/**
	 * 
	 */
	private void passwordEmpty() {
		
		Label labelPasswordEmpty = new Label(I18N.constants.labelPassword());
		getView().setLabelPasswordEmpty(labelPasswordEmpty);
		
		getView().getGridUserData().setWidget(4, 2, labelPasswordEmpty);
		
	}

	/**
	 * @param editDTO
	 */
	private void refreshView(UserEditDTO editDTO) {
		
		getModel().setUserEditDTO(editDTO);
		
		getView().getTextBoxLogin().setText(editDTO.getLogin());
		getView().getTextBoxEMail().setText(editDTO.getEmail());
		getView().getTextBoxForename().setText(editDTO.getForeName());
		getView().getTextBoxPassword().setText(editDTO.getPassword());
		getView().getTextBoxSurename().setText(editDTO.getSureName());
		
		fireEventGlobal(new AddApplicationViewEvent(null, getView().getWidget()));
		
	}

	public void initView() {
		
		VerticalPanel verticalPanelEditUserData = new VerticalPanel();
		getView().setVerticalPanelEditUserData(verticalPanelEditUserData);
		
		Label labelTitle = new Label(I18N.constants.labelEditUserData());
		getView().setLabelTitle(labelTitle);
		
		Grid gridUserData = new Grid(5,3);
		getView().setGridUserData(gridUserData);
		
		Label labelLogin = new Label(I18N.constants.labelLogin());
		getView().setLabelLogin(labelLogin);
		
		Label labelForename = new Label(I18N.constants.labelForename());
		getView().setLabelForename(labelForename);
		
		Label labelSurename = new Label(I18N.constants.labelSurename());
		getView().setLabelSurename(labelSurename);
		
		Label labelEMail = new Label(I18N.constants.labelEmailaddress());
		getView().setLabelEMail(labelEMail);		
		
		Label labelPassword = new Label(I18N.constants.labelPassword());
		getView().setLabelPassword(labelPassword);
				
		TextBox textBoxLogin = new TextBox();
		textBoxLogin.setReadOnly(true);
		getView().setTextBoxLogin(textBoxLogin);
		
		TextBox textBoxForename = new TextBox();
		getView().setTextBoxForename(textBoxForename);
		
		TextBox textBoxSurename = new TextBox();
		getView().setTextBoxSurename(textBoxSurename);
		
		TextBox textBoxEMail = new TextBox();
		getView().setTextBoxEMail(textBoxEMail);
		
		PasswordTextBox  textBoxPassword = new PasswordTextBox();
		getView().setTextBoxPassword(textBoxPassword);
		
		Button buttonSave = new Button(I18N.constants.buttonSave());
		getView().setButtonSave(buttonSave);
		
		buttonSave.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				save();
			}			
		});
		
		Button buttonCancel = new Button(I18N.constants.buttonCancel());
		getView().setButtonCancel(buttonCancel);
		
		buttonCancel.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				History.newItem(HistoryToken.dashboard);
			}
		});
		
		HorizontalPanel horizontalPanelButtons = new HorizontalPanel();
		getView().setHorizontalPanelButtons(horizontalPanelButtons);
		
		/*
		 * View zusammensetzen
		 */
		gridUserData.setWidget(0, 0, labelLogin);
		gridUserData.setWidget(0, 1, textBoxLogin);
		gridUserData.setWidget(1, 0, labelEMail);
		gridUserData.setWidget(1, 1, textBoxEMail);
		gridUserData.setWidget(2, 0, labelForename);
		gridUserData.setWidget(2, 1, textBoxForename);
		gridUserData.setWidget(3, 0, labelSurename);
		gridUserData.setWidget(3, 1, textBoxSurename);
		gridUserData.setWidget(4, 0, labelPassword);
		gridUserData.setWidget(4, 1, textBoxPassword);
		
		horizontalPanelButtons.add(buttonSave);
		horizontalPanelButtons.add(buttonCancel);
		
		verticalPanelEditUserData.add(labelTitle);
		verticalPanelEditUserData.add(gridUserData);
		verticalPanelEditUserData.add(horizontalPanelButtons);
		
	}

	private void save() {
		
		getView().getButtonSave().setEnabled(false);
		getView().getButtonCancel().setEnabled(false);
		
		if (getView().getLabelEMailInvalid() != null) {
			getView().getGridUserData().remove(getView().getLabelEMailInvalid());
		}
		
		if (getView().getLabelPasswordEmpty() != null) {
			getView().getGridUserData().remove(getView().getLabelPasswordEmpty());
		}
		
		UserEditDTO userEditDTO = getModel().getUserEditDTO();
		
		userEditDTO.setEmail(getView().getTextBoxEMail().getText());
		userEditDTO.setForeName(getView().getTextBoxForename().getText());
		userEditDTO.setSureName(getView().getTextBoxSurename().getText());
		userEditDTO.setPassword(getView().getTextBoxPassword().getText());
		
		fireEventGlobal(new UpdateUserEvent(userEditDTO, this));
		
	}
	
	public void initModel() {
		
	}

}
